//
// Created by 33145 on 2023/6/2.
//

#ifndef ROCKET_NET_ADDR_H
#define ROCKET_NET_ADDR_H

#include <memory>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace rocket {

  class NetAddr {
  public:
    typedef std::shared_ptr<NetAddr> ptr;

    virtual ~NetAddr() = default;
    virtual sockaddr* getSockAddr() = 0;
    virtual socklen_t getSockLen() = 0;
    virtual int getFamily() = 0;
    virtual std::string toString() = 0;
    virtual bool checkValid() = 0;
  };

  class IPNetAddr : public NetAddr {
  public:
    IPNetAddr(const std::string& ip, uint16_t port);
    explicit IPNetAddr(const std::string& addr);
    explicit IPNetAddr(sockaddr_in addr);

    sockaddr* getSockAddr() override;
    socklen_t getSockLen() override;
    int getFamily() override;
    std::string toString() override;
    bool checkValid() override;

  private:
    std::string _ip {};
    uint16_t _port {};
    sockaddr_in _addr {};
  };

}

#endif //ROCKET_NET_ADDR_H
